<?php
class ArrowJsInitCode extends ArrowTag{
	
	public function configure(){
		$this->addProperties(array(
			"printHtml" => 1,
			"printFiles" => 0,
			"printCssFiles" => 0,
			"context"=> null,
			"clearRegistredJs" => 1
		));
	}
	
	public function generateOutput(){
		 
		if($this->getProperty("printCssFiles")){
			$files = "";
			foreach(self::getRegistredCssFiles() as $file)
				$files.= '<link rel="stylesheet" type="text/css" href="'.$file.'" />'."\n";
			return $files;
		}
		
		if($this->getProperty("printFiles")){
			$files = "";
			foreach(self::getRegistredJsFiles() as $file)
				$files.= "<script type=\"text/javascript\" src=\"{$file}\" ></script>\n";
				
			self::clearRegistredJsFiles();
			return $files;
		}
		
		if($this->getProperty("printHtml"))
			return "<script type=\"text/javascript\" >\n{$this->generateCode()}\n</script>";
			
		return "<script type=\"text/javascript\" src=\"{$this->getCache()}\" ></script>";
	}
	
	public function getCache(  ){
		$file = array();
		foreach( self::getRegistredJs() as $class => $registred )
			$file[] = $registred["file"];
		
		
		$templateId = ArrowView::getCurrentView()->getTemplateDescriptor()->getKey();
		$cacheFile = ARROW_CACHE_PATH."/template_js_".md5( serialize( $file).self::getRegistredJs().$this->getProperty("context").$templateId ).".js";		
		$generateCache = false;
		if( file_exists( $cacheFile ) ){
			if(!SUPER_CACHE){
				$cacheFileMTime = filemtime( $cacheFile );
				if( is_array($file) ){
					foreach( $file as $eFile ){
						if( $cacheFileMTime < filemtime($eFile) ){
							$generateCache = true;
						}
					}
				}else{
					if( $cacheFileMTime < filemtime($file) )
							$generateCache = true;
				}
			}
		}else{
			$generateCache = true;
		}
		
		if( $generateCache )
			file_put_contents($cacheFile, $this->generateCode());
		
		return $cacheFile;
	}
	
	public function generateCode(){
		
		$str = "";
		$str.= "if (typeof($) != 'undefined'){ ";
		$str.= "var msie = $.browser.msie;\n";
		$str.= "var old = $.browser.version.substr(0,1)<\"8\";\n";
		$str.= "}else{var msie=true; var old = true; }"; 
		foreach( self::getRegistredJs() as $class => $registred ){
			$content = file_get_contents($registred["file"]);
			$str.= $content."\n"; 
			foreach($registred["selector"] as $selectorData){
				$selector = $selectorData["value"];
				$data = json_encode($selectorData["data"]);
				$str.= "var ctrlInitFunction = function(context, selector){\n";
				$str.= "if(context == false) context = $(document);\n"; 
				$str.= "ctrlInit".$class."(context, '".$selector."', ".$data.");\n};\n";
				$str.= "if (msie && old)\n\t $(function(){ctrlInitFunction('{$this->getProperty("context")}');});\n else{ctrlInitFunction('{$this->getProperty("context")}');}\n";
				
				$str.= "CtrlAjax.addGlobalLoadCallback(ctrlInitFunction, '".$selector."');\n";
			}
		}
		foreach( self::getRegistredEvents() as $source => $event)
			$str.= "\n $source";
			
		if( $this->getProperty("clearRegistredJs") )
			self::clearRegistredJs();
		
		return $str;
	}
}
?>